package com.corecloud.service.statisticalCenter.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corecloud.entity.SysAreaEntity;
import com.corecloud.entity.statisticalCenter.SchoolGradationEntity;
import com.corecloud.mapper.mysql.statisticalCenter.StatisticalSchoolMapper;
import com.corecloud.mapper.mysql.SysAreaMapper;
import com.corecloud.service.statisticalCenter.StatisticalSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学校信息统计
 */
@Service("statisticalSchoolService")
public class StatisticalSchoolServiceImpl implements StatisticalSchoolService {

    @Autowired
    private StatisticalSchoolMapper statisticalSchoolMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;
    // ZHF
    @Autowired
    private SysAreaMapper areaMapper;


    /**
     * dept：学校统计
     */

    // 学校层次统计
    @Override
    public List<SchoolGradationEntity> schoolGradationYears(SchoolGradationEntity param) {
        if(!StringUtils.isEmpty(param.getDataYearStart()) && !StringUtils.isEmpty(param.getDataYearEnd()) ){
            param.setYears(getYears(Integer.parseInt(param.getDataYearStart()),Integer.parseInt(param.getDataYearEnd())));
        }
        // 数据结构：year：年份；schoolLevel：类型；count类型个数
        List<SchoolGradationEntity> schoolGradationEntities = statisticalSchoolMapper.schoolGradationYears(param);

        /**
        List<SchoolGradationEntity> returnSchools = new ArrayList<>();

        for (SchoolGradationEntity schoolGradationEntity:schoolGradationEntities) {
            SchoolGradationEntity listSchool = new SchoolGradationEntity();
            // 两种情况，1、list没有数据；2、list有数据
            if(returnSchools.size()>0){
                boolean isHave = false;
                // 有数据，判断是否是相同年份的数据
                for (SchoolGradationEntity retSchool:returnSchools){
                    // 存在，根据类型赋值
                    if(schoolGradationEntity.getYear().equals(retSchool.getYear())){
                        // 判断中职还是高职,进行存放数据
                        if(schoolGradationEntity.getSchoolLevel().equals(1)){
                            retSchool.setSecondaryVocational(schoolGradationEntity.getCount());
                        }else if(schoolGradationEntity.getSchoolLevel().equals(2)){
                            retSchool.setHigherVocational(schoolGradationEntity.getCount());
                        }
                        isHave = true;
                        break;
                    }
                }
                if(!isHave){
                    listSchool.setYear(schoolGradationEntity.getYear());
                    if(schoolGradationEntity.getSchoolLevel().equals(1)){
                        listSchool.setSecondaryVocational(schoolGradationEntity.getCount());
                    }else if(schoolGradationEntity.getSchoolLevel().equals(2)){
                        listSchool.setHigherVocational(schoolGradationEntity.getCount());
                    }
                    returnSchools.add(listSchool);
                }
            }else{
                // 没有数据，第一个先添加数据
                listSchool.setYear(schoolGradationEntity.getYear());
                if(schoolGradationEntity.getSchoolLevel().equals(1)){
                    listSchool.setSecondaryVocational(schoolGradationEntity.getCount());
                }else if(schoolGradationEntity.getSchoolLevel().equals(2)){
                    listSchool.setHigherVocational(schoolGradationEntity.getCount());
                }
                returnSchools.add(listSchool);
            }
        }

**/
        Collections.reverse(schoolGradationEntities);

        List<SchoolGradationEntity> schoolGradationList = new ArrayList<>();
        // 获取上一个的数据并相加
        for(int i=0;i<schoolGradationEntities.size();i++){
            SchoolGradationEntity schoolGradationEntity = schoolGradationEntities.get(i);
            if(i!=0){
                SchoolGradationEntity lastSchool = schoolGradationEntities.get(i - 1);
                schoolGradationEntity.setSecondaryVocational(schoolGradationEntity.getSecondaryVocational()+lastSchool.getSecondaryVocational());
                schoolGradationEntity.setHigherVocational(schoolGradationEntity.getHigherVocational()+lastSchool.getHigherVocational());
                schoolGradationEntity.setSum(schoolGradationEntity.getSum()+lastSchool.getSum());
            }
            schoolGradationList.add(schoolGradationEntity);
        }

        String areaName = "";
        if(param != null && !StringUtils.isEmpty(param.getAreaId())){
            SysAreaEntity sysAreaEntity = sysAreaMapper.selectOne(new QueryWrapper<SysAreaEntity>().eq("area_id", param.getAreaId()));
            areaName = sysAreaEntity.getAreaName();
        }

        String a = areaName;
        schoolGradationList.stream().forEach(item -> {
            item.setSum(item.getSecondaryVocational()+item.getHigherVocational());
            item.setAreaName(a);
        });

        Collections.reverse(schoolGradationList);

        return schoolGradationList;
    }

    // 学校层次统计（按地市）
    @Override
    public List<SchoolGradationEntity> schoolGradationAreas(SchoolGradationEntity info) {
        // 当前年份
        Calendar calendar = Calendar.getInstance();
        Integer currentYear = calendar.get(Calendar.YEAR);
        info.setYear(info.getYear()==null||info.getYear().equals("")?currentYear.toString():info.getYear());
        List<SchoolGradationEntity> schoolGradationEntities = statisticalSchoolMapper.schoolGradationAreas(info);
        Integer dateYear = Integer.parseInt(info.getYear())-1;
        info.setYear(dateYear.toString());
        // 去年的数据
        List<SchoolGradationEntity> lastYearDatas = statisticalSchoolMapper.schoolGradationAreas(info);

        schoolGradationEntities = schoolGradationEntities.stream().map(schoolGradationEntity -> {

            // 中职
            Integer secondaryVocational = schoolGradationEntity.getSecondaryVocational();
            // 高职
            Integer higherVocational = schoolGradationEntity.getHigherVocational();
            // 总数
            Integer sum = schoolGradationEntity.getSum();

            SchoolGradationEntity lastYearData = lastYearDatas.stream().filter(data -> data.getAreaName().equals(schoolGradationEntity.getAreaName())).findFirst().orElse(new SchoolGradationEntity());
            // 中职
            schoolGradationEntity.setSecondaryTrend(lastYearData.getSecondaryVocational() < secondaryVocational ? 1 : lastYearData.getSecondaryVocational() > secondaryVocational ? 2 : 0);
            // 高职
            schoolGradationEntity.setHigherTrend(lastYearData.getHigherVocational() < higherVocational ? 1 : lastYearData.getHigherVocational() > higherVocational ? 2 : 0);
            // 总数
            schoolGradationEntity.setTrend(lastYearData.getSum() < sum ? 1 : lastYearData.getSum() > sum ? 2 : 0);

            return schoolGradationEntity;
        }).collect(Collectors.toList());

        Integer year = Integer.parseInt(info.getYear())+1;
        schoolGradationEntities.stream().forEach(item -> {
            item.setYear(year.toString());
        });

        return schoolGradationEntities;
    }

    // 生成日期范围区间的年份
    public List<Integer> getYears(Integer dataYearStart,Integer dataYearEnd){
        List<Integer> arr = new ArrayList<>();
        for (int i=dataYearEnd-1;i>=dataYearStart;i--){
            arr.add(i);
        }
        return arr;
    }

}
